package com.example.devicemanage.controller;

import com.example.devicemanage.entity.Apply;
import com.example.devicemanage.service.inter.ApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ApplyController {
    @Autowired
    ApplyService applyService;

    @RequestMapping(path = "/apply_list", method = RequestMethod.GET)
    public String getApply_list(HttpServletRequest httpServletRequest) {
        int currentPage = 1;
        int count = 6;
        int totalPage = 0;
        String targetPage = httpServletRequest.getParameter("targetPage");
        if (targetPage != null) {
            currentPage = Integer.parseInt(targetPage);
        }
        List<Apply> list1;
        list1 = applyService.queryAllApply();
        List<Apply> list = new ArrayList<>();
        if (list1.size() % 6 == 0) {
            totalPage = list1.size() / 6;
        } else {
            totalPage = list1.size() / 6 + 1;
        }
        if (currentPage * count <= list1.size()) {
            for (int i = 0; i < 6; i++) {
                list.add(list1.get((currentPage - 1) * count + i));

            }
        } else {
            int m = list1.size() - (currentPage - 1) * count;
            for (int j = 0; j < m; j++) {
                list.add(list1.get(list1.size() - m + j));
            }
        }


        httpServletRequest.setAttribute("list", list);
        httpServletRequest.setAttribute("totalPage", totalPage);
        httpServletRequest.setAttribute("applyNum", list1.size());
        httpServletRequest.setAttribute("currentPage", currentPage);


        return "apply_list";
    }


    @RequestMapping(path = "/equip_apply", method = RequestMethod.GET)
    public String getEquip_apply() {
        return "equip_apply";
    }

    @ResponseBody
    @RequestMapping(path = "/apply_save")
    public String saveApply(HttpServletRequest httpServletRequest) {

        String ename = httpServletRequest.getParameter("ename");
        String category = httpServletRequest.getParameter("category");
        String model = httpServletRequest.getParameter("model");
        String spec = httpServletRequest.getParameter("spec");
        String price = httpServletRequest.getParameter("price");
        String uname = httpServletRequest.getParameter("uname");
        String num = httpServletRequest.getParameter("num");

        Apply apply = new Apply();

        apply.setEname(ename);
        apply.setCategory(category);
        apply.setModel(model);
        apply.setSpec(spec);
        apply.setPrice(Integer.parseInt(price));
        apply.setUname(uname);
        apply.setNum(Integer.parseInt(num));

        return applyService.saveApply(apply) == 1 ? "保存成功" : "保存失败";
    }
    @ResponseBody
    @RequestMapping(path = "/apply_update")
    public String updateApply(HttpServletRequest httpServletRequest) {
        String aid = httpServletRequest.getParameter("aid");
        String ename = httpServletRequest.getParameter("ename");
        String category = httpServletRequest.getParameter("category");
        String model = httpServletRequest.getParameter("model");
        String spec = httpServletRequest.getParameter("spec");
        String price = httpServletRequest.getParameter("price");
        String uname = httpServletRequest.getParameter("uname");
        String num = httpServletRequest.getParameter("num");

        Apply apply = new Apply();
        apply.setAid(Integer.parseInt(aid));
        apply.setEname(ename);
        apply.setCategory(category);
        apply.setModel(model);
        apply.setSpec(spec);
        apply.setPrice(Integer.parseInt(price));
        apply.setUname(uname);
        apply.setNum(Integer.parseInt(num));

        return applyService.updateApply(apply) == 1 ? "保存成功" : "保存失败";
    }
    @ResponseBody
    @RequestMapping(path = "/apply_pass")
    public String passApply(int aid) {
        applyService.passApply(aid);
        return "更改成功";
    }

    @ResponseBody
    @RequestMapping(path = "/apply_delete")
    public String deleteApply(int aid) {

        return applyService.deleteApply(aid) ? "删除成功" : "aid=" + aid + "不存在 Something wrong";
    }
}
